package com.mmsj;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author ChuXuan
 */
public class RegisterActivity extends AppCompatActivity {

    private EditText etUserName;
    private EditText etPassword;
    private EditText etConfirmPassword;
    private EditText etPhone;
    private Button btnRegister;
    private TextView tvLogin;

    private String strUserName;
    private String strPassword;
    private String strConfirmPassword;
    private String strPhone;

    SQLiteDatabase myDataBase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化UI
        setContentView(R.layout.activity_register);
        etUserName = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        etConfirmPassword = findViewById(R.id.et_confirm_password);
        etPhone = findViewById(R.id.et_phone);
        btnRegister = findViewById(R.id.btn_register);
        tvLogin = findViewById(R.id.tv_login);

        myDataBase = MyDBHelper.getInstance(this).getWritableDatabase();

        // 注册按钮的点击事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取输入框的输入文本
                strUserName = etUserName.getText().toString().trim();
                strPassword = etPassword.getText().toString().trim();
                strConfirmPassword = etConfirmPassword.getText().toString().trim();
                strPhone = etPhone.getText().toString().trim();
                // 在以下空白处补充代码
                if (strUserName.isEmpty() || strPassword.isEmpty() || strConfirmPassword.isEmpty() || strPhone.isEmpty()) {
                    // 输入框为空时，弹出提示
                    Toast.makeText(RegisterActivity.this, "检查输入", Toast.LENGTH_SHORT).show();
                } else if (!strPassword.equals(strConfirmPassword)) {
                    // 两次输入的密码不一致时，弹出提示
                    Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    // 输入框都不为空且两次输入的密码一致时，执行注册操作
                    // 检查用户名
                    String querySql = "SELECT * FROM user WHERE username = ? OR phone = ?";
                    @SuppressLint("Recycle") Cursor myCursor = myDataBase.rawQuery(querySql, new String[]{strUserName, strPhone});
                    if (myCursor.getCount() > 0) {
                        // 用户名已存在，弹出提示
                        Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                    } else {
                        // 插入数据到数据库中
                        String insertSql = "INSERT INTO user (username, password, phone) VALUES (?, ?, ?)";
                        myDataBase.execSQL(insertSql, new String[]{strUserName, strPassword, strPhone});
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        // 跳转至登录页面
                        Intent myIntent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(myIntent);
                        finish();
                    }
                }


            }
        });

        // 点击登录的点击事件，跳转至登录页面
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(myIntent);
            }
        });


    }
}